package com.woyou.ui.adapter;

import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.Coupon;
import com.woyou.bean.Gift;
import com.woyou.bean.IChoose;
import com.woyou.bean.ShoppingCarService;
import com.woyou.model.ShoppingCarModel;
import com.woyou.model.ShoppingCarModel_;
import com.woyou.utils.ParseUtils;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConfirmOrderAdapter extends BaseAdapter {

	private Context context;
	private List<Object> data;
	private ShoppingCarModel shoppingCarModel;
	
	public ConfirmOrderAdapter(Context context,List<Object> data) {
		this.context = context;
		this.data = data;
		shoppingCarModel=ShoppingCarModel_.getInstance_(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_pricelist_lv, null);
			viewHolder.name=(TextView)convertView.findViewById(R.id.item_pricelist_lv_name);
			viewHolder.num=(TextView)convertView.findViewById(R.id.item_pricelist_lv_num);
			viewHolder.price=(TextView)convertView.findViewById(R.id.item_pricelist_lv_price);
			viewHolder.adjust=(LinearLayout)convertView.findViewById(R.id.item_goods_adjust);
			viewHolder.prop=(TextView)convertView.findViewById(R.id.item_goods_prop);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.adjust.setVisibility(View.GONE);
		Object obj=data.get(position);
		if(obj instanceof IChoose){
			IChoose cg= (IChoose) obj;
			if(cg.getChosenOptList().size()>0){
				shoppingCarModel.restName(cg);
				viewHolder.adjust.setVisibility(View.VISIBLE);
				viewHolder.prop.setText(Html.fromHtml(cg.getNames()));
			}else{
				viewHolder.adjust.setVisibility(View.GONE);
			}
			viewHolder.name.setText(Html.fromHtml(cg.getgName()));
			viewHolder.num.setText("×"+cg.getChosenNum());
			viewHolder.price.setText(Html.fromHtml("<small>￥</small>"+ParseUtils.String2Money(cg.getPrice()*cg.getChosenNum())));
		}else if(obj instanceof ShoppingCarService){
			ShoppingCarService sc=(ShoppingCarService)obj;
			viewHolder.name.setText(sc.getName());
			viewHolder.num.setText("");
			viewHolder.price.setText(Html.fromHtml("<small>￥</small>"+sc.getPrice()));
		}else if(obj instanceof Gift){
			Gift gift=(Gift)obj;
			viewHolder.name.setText(gift.getaName());
			if(gift.getPrice()==0){
				viewHolder.num.setText("×"+gift.getQty());	
				viewHolder.price.setText("赠送");
			}else if(gift.getPrice()>0){
				viewHolder.num.setText("×"+gift.getQty());	
				viewHolder.price.setText(Html.fromHtml("<small>￥</small>"+ParseUtils.String2Money(gift.getPrice()*gift.getQty())));
			}else{
				viewHolder.num.setText("");	
				viewHolder.price.setText(Html.fromHtml("-<small>￥</small>"+ParseUtils.String2Money(Math.abs(gift.getPrice()*gift.getQty()))));
			}
		}else if(obj instanceof Coupon){
			Coupon coupon=(Coupon)obj;
			viewHolder.name.setText("优惠券");
			viewHolder.num.setText("×1");
			viewHolder.price.setText(Html.fromHtml("-<small>￥</small>"+ParseUtils.String2Money(Float.valueOf(coupon.getcValue()))));
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView name;
		TextView unit;
		TextView price;
		TextView num;
		LinearLayout adjust;
		TextView prop;
	}
}
